package 可重复发送;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 在线聊天室：客户端
 * 目标：实现以一个客户收发多条消息
 */
public class Chat {
    public static void main(String[] args) throws IOException {
        System.out.println("-----------Server------------");
        //1.指定端口，打开一个服务端
        ServerSocket chat = new ServerSocket(8888);
        //2.阻塞式接收accept()
        Socket socket = chat.accept();
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        boolean isRunning = true;
        System.out.println("一个客户端建立了连接");
        while (isRunning){
            //3.接收消息
            String mag = dis.readUTF();
            //System.out.println(mag);
            //4.返回消息
            dos.writeUTF(mag);
            dos.flush();
            if (mag == null){
                break;
            }
        }
        //5.关闭、释放资源
        chat.close();
        socket.close();
        dis.close();
        dos.close();

    }
}
